<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
use Session;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';
    protected $username;


    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
    }
    /**
     * 重写登录视图页面
     * @author 晚黎
     * @date   2016-09-05T23:06:16+0800
     * @return [type]                   [description]
     */
    public function showLoginForm()
    {
        return view('web.auth.login');
    }
    /**
     * 自定义认证驱动
     * @author 晚黎
     * @date   2016-09-05T23:53:07+0800
     * @return [type]                   [description]
     */
    protected function guard()
    {
        return auth()->guard('web');
    }

    /**
     * Log the user out of the application.
     *
     * @param  Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout()
    {
        $this->guard('web')->logout();

        request()->session()->flush();

        request()->session()->regenerate();

        return redirect('/login');
    }
	
	public function weblogin($id){
		if(Auth::guard('admin')->user()){
			Auth::guard('web')->loginUsingId($id);
			return redirect('/');
		}else{
			return redirect('/logout');
		}
	}

	public function login(Request $request) {
        if(Session::get('milkcaptcha') != $request->yzm){
        	return redirect('/login')->withErrors('验证码有误！！！');die;
        }
	    if ($this->guard()->attempt(['email' =>$request->input("username"), 'password' => $request->password])) {
	        return $this->sendLoginResponse($request);
	    }
	    
	    if ($this->guard()->attempt(['mobile' =>$request->only("username"), 'password' => $request->password])) {
	        return $this->sendLoginResponse($request);
	    }
	    
	    if ($this->guard()->attempt(['name' =>$request->only("username"), 'password' => $request->password])) {
	        return $this->sendLoginResponse($request);
	    }
	
	    return $this->sendFailedLoginResponse($request);
	}
}
